package com.tuanbiao.ui.auth.adapter;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.abiao.lib_utils.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hzjq.data.minbao.entity.AuthEntity;
import com.tuanbiao.R;
import com.tuanbiao.base.action.ResourcesAction;
import com.tuanbiao.utils.LibCollections;

import java.util.List;

/**
 * @author abiao
 * @since 2022/4/22 17:05
 */
public class AuthListAdapter extends BaseQuickAdapter<AuthEntity, BaseViewHolder> implements ResourcesAction {
    public AuthListAdapter() {
        super(R.layout.item_auth_list);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, AuthEntity entity) {
        if (null != entity.getLgs()) {
            List<AuthEntity.Lgs.Lg> lg = entity.getLgs().getLg();
            holder.setText(R.id.tv_count, getString(R.string.auth_list_count, String.valueOf(LibCollections.size(lg))))
                    .setText(R.id.tv_time, getString(R.string.auth_list_apply_time, entity.getSqrq()));
            if (LibCollections.isEmpty(lg)) {
                holder.setVisible(R.id.tv_status, true);
                holder.setText(R.id.tv_validity_period, getString(R.string.auth_list_validity_period, "--"));
            } else {
                holder.setVisible(R.id.tv_status, statusVisibility(entity));
                String yxq = entity.getLgs().getLg().get(0).getYxq();
                if(TextUtils.isEmpty(yxq)){
                    holder.setText(R.id.tv_validity_period, getString(R.string.auth_list_validity_period, "已失效"));
                } else {
                    holder.setText(R.id.tv_validity_period, getString(R.string.auth_list_validity_period, yxq));
                }
            }
        } else {
            holder.setVisible(R.id.tv_status, true);
            holder.setText(R.id.tv_count, getString(R.string.auth_list_count, "0"))
                    .setText(R.id.tv_time, getString(R.string.auth_list_apply_time, entity.getSqrq()));
            holder.setText(R.id.tv_validity_period, getString(R.string.auth_list_validity_period, "--"));
        }
    }

    private boolean statusVisibility(AuthEntity entity) {
        if (TextUtils.isEmpty(entity.getLgs().getLg().get(0).getYxq())) {
            return false;
        }
        return TimeUtils.getNowMills() > TimeUtils.string2Millis(entity.getLgs().getLg().get(0).getYxq());
    }
}
